/**
 * 添加附加物品接口
 */

//引入Q模块
var Q = require('q');
//引入公共函数
var util = require('../../util');

//addGoods接口入口文件
var index = function(getData, postData, config, _redis, db) {
	let deferred = Q.defer();
	if(postData.goodsName && postData.goodsPrice) {
		let goodsId = '';
		util.createRandomStr(6).then(function(_goodsId) {
			goodsId = _goodsId;
			return db.insert('attachGoods', {
				goodsId: goodsId,
				goodsName: postData.goodsName,
				goodsPrice: postData.goodsPrice * 100,
				goodsCount: postData.goodsCount,
				goodsForce: (postData.goodsForce == 0 ? false : true),
				goodsChecked: (postData.goodsChecked == 0 ? false : true),
				timestamp: Date.now()
			});
		}, function() {
			//生成随机字符串失败
			deferred.reject('-0069');
		}).then(function() {
			deferred.resolve({
				goodsId: goodsId,
				timestamp: Date.now()
			});
		}, function() {
			//添加附加物品失败
			deferred.reject('-0180');
		});
	}
	else {
		//添加附加物品接口参数不足
		deferred.reject('-0179');
	}
	return deferred.promise;
}

//暴露接口
module.exports = {
	index: index
}